<?php
namespace Home\Controller;
use Think\Page;
class ConcertController extends HomeController
{
    //演唱会列表信息
    public function index($p = 1)
    {
        $category_id = I('category_id','');
        if ($category_id!=''){
            if ($category_id<148||$category_id>150){
                $this->error("404 Not Found");
            }
            $map['category_id'] = $category_id;
            $categoryName = M('Category')->where("id='$category_id'")->field('title')->find();
        }else{
            $categoryName['title'] = "演唱会";
        }
        $map['status'] = 1;

        $concerts = M('ConcertLists');
        $page_size = 20;
        $count = $concerts->where($map)->count();
        $page = new Page($count,$page_size);
        $list = $concerts->where($map)->order('create_time desc')->page($p . ',' . $page_size)->select();
        $this->title = $categoryName['title'];
        $this->assign('list',$list);
        $this->assign('page',$page->show());
        $this->display();
    }

    public function detail($id = 0){
        $shows = $this->getShows();
        $onlines = $this->getOnlines();
        if ($id){
            $concert = M('ConcertLists')->where("id='$id'")->find();
            $document = M('Document')->where("id=".$concert['cid'])->find();
            $content = M('DocumentConcert')->where("id=".$concert['cid'])->find();
            $category = M('Category')->where("id=".$document['category_id'])->field('id,title')->find();
            $this->assign('concert',$concert);
            $this->assign('content',$content);
            $this->assign('document',$document);
            $this->assign('category',$category);
            $this->assign('shows',$shows);
            $this->assign('onlines',$onlines);
            $this->display();
        }else{
            $this->error('找不到该商品!');
        }
    }

    function getShows(){
        $concerts = M('ConcertLists')->where('status=1')->order('hots desc')->limit(3)->select();
        return $concerts;
    }

    function getOnlines(){
        $lives = M('Live')->where('status=1')->order('create_time desc')->limit(3)->select();
        return $lives;
    }

    public function getBookings($cid = 0){
        $map['status'] = array('neq',0);
        $map['cid'] = $cid;
        $bookings = M('Bookings')->where($map)->field('seats')->select();
        $booking = array();
        foreach ($bookings as $i => $v){
            array_push($booking,$v['seats']);
        }
        $ajaxreturn = array('booking'=>$booking);
        $this->ajaxReturn($ajaxreturn);
    }
}